package com.liuyang.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuyang.common.Result;
import com.liuyang.common.TableResult;
import com.liuyang.entity.Disasterimg;
import com.liuyang.entity.Materials;
import com.liuyang.entity.Materialsimg;
import com.liuyang.service.DisasterimgService;
import com.liuyang.service.MaterialsService;
import com.liuyang.service.MaterialsimgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者:caleb
 * @since 2022-02-23
 */
@RestController
@RequestMapping("/system/materials")
public class MaterialsController {

    @Autowired
    private MaterialsService materialsService;

    @Autowired
    private MaterialsimgService materialsimgService;

    @Autowired
    private DisasterimgService disasterimgService;

    /**
     * 我要求助, 添加需要申请的物资信息
     * @param map
     * @return
     */
    @PostMapping("/addMaterials")
    public Result addMaterials(@RequestBody Map map) {
        if(map.size() == 0) {
            return Result.fail("申请失败");
        }
        Snowflake snowflake = new Snowflake(2,3);
        Materials materials = new Materials();
        String id = snowflake.nextIdStr();
        String applicationTime = DateUtil.format(new Date(), "yyyy-MM-dd");
        materials.setId(id);
        materials.setApplicationTime(applicationTime);
        materials.setStatus("3"); // 处于待帮助状态
        BeanUtil.fillBeanWithMap(map, materials, false);
        materialsService.saveOrUpdate(materials); // 添加物资
        String imgIds = map.get("imgIds").toString();
        List<String> imgIdsList = StrUtil.split(imgIds, ',');
        for(String imgId : imgIdsList) {
//            Materialsimg materialsimg = new Materialsimg();
//            materialsimg.setId(imgId);
//            materialsimg.setMaterialsId(id);
//            materialsimgService.saveOrUpdate(materialsimg);
            Disasterimg disasterimg = new Disasterimg();
            disasterimg.setId(imgId);
            disasterimg.setMaterialsId(id);
            disasterimgService.saveOrUpdate(disasterimg);
        }
        return Result.succ("申请成功");
    }

    /**
     * 我来帮助, 状态3, 3表示处于待接收帮助
     * @return
     */
    @GetMapping("/queryMaterials")
    public TableResult queryMaterials(String applicantId) {
        List<Materials> materialsList = materialsService.list(new QueryWrapper<Materials>().eq("status", "3").ne("applicantId", applicantId));
        TableResult tableResult = new TableResult(0, "ok",materialsList.size(),materialsList);
        return tableResult;
    }

    /**
     * 我的申请 - 求助申请
     * @param applicantId
     * @return
     */
    @GetMapping("/queryMaterialsByApplicantId")
    public TableResult queryMaterialsByApplicantId(String applicantId) {
        List<Materials> list = materialsService.list(new QueryWrapper<Materials>().eq("applicantId", applicantId));
        TableResult tableResult = new TableResult(0,"ok",list.size(),list);
        return tableResult;
    }

    /**
     * 我的申请 - 帮助申请
     * @param helperId
     * @return
     */
    @GetMapping("/queryMaterialsByHelperId")
    public TableResult queryMaterialsByHelperId(String helperId) {
        List<Materials> list = materialsService.list(new QueryWrapper<Materials>().eq("helperId", helperId));
        TableResult tableResult = new TableResult(0,"ok",list.size(),list);
        return tableResult;
    }

    /**
     * 物资详情
     * @param id
     * @return
     */
    @GetMapping("/queryMaterialsById")
    public Result queryMaterialsById(String id) {
        Materials materials = materialsService.queryMaterialsById(id);
        return Result.succ(materials);
    }

    /**
     * 我来帮助, 接受需要帮助物资, 状态置为 0, 表示从状态3变为0, 当前状态处于待审核状态
     * @param id
     * @return
     */
    @PostMapping("/editReceiveMaterialsById")
    public Result editReceiveMaterialsById(String id, String helperId, String helpType, String imgIds) {
        Materials materials = new Materials();
        materials.setId(id);
        materials.setStatus("0");
        materials.setHelperId(helperId);
        materials.setHelpType(helpType);

        List<String> imgIdsList = StrUtil.split(imgIds, ',');
        for(String imgId : imgIdsList) {
            Materialsimg materialsimg = new Materialsimg();
            materialsimg.setId(imgId);
            materialsimg.setMaterialsId(id);
            materialsimgService.saveOrUpdate(materialsimg);
        }
        materialsService.saveOrUpdate(materials);
        return Result.succ("申请帮助成功...");
    }

    /**
     * 求助审核, 对物资状态0, 进行查询
     * @return
     */
    @GetMapping("/queryMaterialsCheckApply")
    public TableResult queryMaterialsCheckApply() {
        List<Materials> list = materialsService.list(new QueryWrapper<Materials>().eq("status", "0"));
        TableResult tableResult = new TableResult(0, "ok",list.size(),list);
        return tableResult;
    }

    @PostMapping("/editMaterialsCheckApply")
    public Result editMaterialsCheckApply(String id, String status, String checkIdea) {
        Materials materials = new Materials();
        materials.setId(id);
        materials.setStatus(status);
        materials.setCheckIdea(checkIdea);
        materialsService.saveOrUpdate(materials);
        return Result.succ("1".equals(status)? "审核通过": "审核不通过");
    }
}
